﻿using System;
using System.Collections.Generic;
using System.Text;

namespace D3Calc
{
    public class Profile
    {
        public int ElitesKilled
        {
            get
            {
                return this.elitesKilled;
            }
        }

        public List<Hero> HeroList
        {
            get
            {
                return this.heroList;
            }
        }

        public int MonstersKilled
        {
            get
            {
                return this.monstersKilled;
            }
        }

        public string Apiurl
        {
            get { return apiurl; }
            set { apiurl = value; }
        }

        public string Tag
        {
            get { return tag; }
            set { tag = value; }
        }

        private Profile()
        {
            this.tag = string.Empty;
            this.apiurl = string.Empty;
            this.heroList = new List<Hero>();
        }

        public Profile(string tag, string bn)
            : this()
        {
            this.tag = tag;
            this.apiurl = string.Format("http://" + bn + ".battle.net/api/d3/profile/{0}/index", tag.Replace("#", "-"));
            string json = BNHelper.GetJson(this.apiurl);
            if (json == "error")
            {
                //MessageBox.Show("抱歉，获取的英雄列表失败\n\n可能原因参考：\n  1.战网Tag错误(大小写)\n  2.战网服务器超载\n  3.该服务器未建立人物\n  4.本地网络异常", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                string lenguage;
                string heroesStart = "\"heroes\" : [ ";
                string heroesEnd = "]";
                try
                {
                    int index = json.IndexOf(heroesStart);
                    int num2 = json.IndexOf(heroesEnd, index);
                    string[] heroesArray = json.Substring(index + heroesStart.Length, ((num2 - index) - heroesStart.Length) - heroesEnd.Length).Trim().Split(new string[] { ", {" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string jsonHero in heroesArray)
                    {
                        Hero hero = new Hero();
                        hero.Name = BNHelper.GetValue(jsonHero, "\"name\" : ");
                        hero.ID = BNHelper.GetValue(jsonHero, "\"id\" : ");
                        hero.Level = BNHelper.GetValue(jsonHero, "\"level\" : ");
                        hero.Hardcore = BNHelper.GetValue(jsonHero, "\"hardcore\" : ");
                        hero.ParagonLevel = BNHelper.GetValue(jsonHero, "\"paragonLevel\" : ");
                        hero.Gender = BNHelper.GetValue(jsonHero, "\"gender\" : ");
                        hero.Dead = BNHelper.GetValue(jsonHero, "\"dead\" : ");
                        hero.Class = BNHelper.GetValue(jsonHero, "\"class\" : ");

                        long num3 = Convert.ToInt64(BNHelper.GetValue(jsonHero, "\"last-updated\" : ")) * 0x3e8;
                        num3 += 0x1b77400;
                        DateTime time = new DateTime(0x7b2, 1, 1);
                        long ticks = time.Ticks + (num3 * 0x2710);
                        hero.LastUpdated = new DateTime(ticks);

                        if (bn == "tw")
                        {
                            lenguage = "zh";
                        }
                        else if (bn == "kr")
                        {
                            lenguage = "ko";
                        }
                        else
                        {
                            lenguage = "en";
                        }
                        hero.BattleNetUrl = string.Format("http://" + bn + ".battle.net/d3/" + lenguage + "/profile/{0}/hero/" + hero.ID, tag.Replace("#", "-"));

                        this.heroList.Add(hero);
                    }

                    heroesStart = "\"kills\" : ";
                    heroesEnd = "}";
                    this.monstersKilled = Convert.ToInt32(BNHelper.GetValue(json, "\"monsters\" : "));
                    this.elitesKilled = Convert.ToInt32(BNHelper.GetValue(json, "\"elites\" : "));
                }
                catch
                {
                    //MessageBox.Show("抱歉，获取的英雄信息失败。\n\n可能原因参考：\n  1.战网Tag错误(大小写)\n  2.战网服务器超载\n  3.该服务器未建立人物\n  4.本地网络异常", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private string apiurl;
        private int elitesKilled;
        private List<Hero> heroList;
        private int monstersKilled;
        private string tag;
    }
}
